/*
 * UserInfo.java
 *Create on 20080114
 *
 */

package com.gln.bloger.pojos;

import java.io.Serializable;
import java.util.Date;

import com.gln.bloger.util.PojoUtil;

/**
 *
 * @author renxj
 */
public class User extends Entity implements Serializable {
   
    static final long serialVersionUID = -6354583200913127874L;
    private String  id;
    private String  userName;
    private String  password;
    private String  fullName;
    private String  email;
    private Date    dateCreated;
    private Boolean enabled = Boolean.TRUE;
    private String qq;
    private String msn;
//    private Set<Role> roles = new HashSet<Role>();
    public User() {}
    
    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getMsn() {
		return msn;
	}

	public void setMsn(String msn) {
		this.msn = msn;
	}
    
    public User( User otherData ) {
        setData(otherData);
    }
    
    
    public Boolean getEnabled() {
        return this.enabled;
    }
    
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    
    /** Id of the User.
     * Not remote since primary key may be extracted by other means.
     *
     */
    public String getId() {
        return this.id;
    }
    
    public void setId( String id ) {
        this.id = id;
    }
    
    /**
     *  User name 
     */
    public String getUserName() {
        return this.userName;
    }
    
    public void setUserName( String userName ) {
        this.userName = userName;
    }
    
    /**
     * Get password.
     * If password encryption is enabled, will return encrypted password.
     *
     */
    public String getPassword() {
        return this.password;
    }
    /**
     * Set password.
     * If password encryption is turned on, then pass in an encrypted password.
     */
    public void setPassword( String password ) {
        this.password = password;
    }
    
    /**
     * Full name of the user.
     *
     */
    public String getFullName() {
        return this.fullName;
    }
    public void setFullName( String fullName ) {
        this.fullName = fullName;
    }

    //------------------------------------------------------------------- citizenship
    public String toString() {
        StringBuffer str = new StringBuffer("{");
        
        str.append("id=" + id + " ");
        str.append("userName=" + userName + " ");
        str.append("password=" + password + " ");
        str.append("fullName=" + fullName + " ");
        str.append("email=" + email + " ");
        str.append("qq=" + qq + " ");
        str.append("msn=" + msn + " ");
        str.append("dateCreated=" + dateCreated + " ");
        str.append('}');
        
        return(str.toString());
    }
    
    public boolean equals( Object pOther ) {
        if (pOther instanceof User) {
            User lTest = (User) pOther;
            boolean lEquals = true;
            lEquals = PojoUtil.equals(lEquals, this.getId(), lTest.getId());
            lEquals = PojoUtil.equals(lEquals, this.getUserName(), lTest.getUserName());
            lEquals = PojoUtil.equals(lEquals, this.getPassword(), lTest.getPassword());
            lEquals = PojoUtil.equals(lEquals, this.getFullName(), lTest.getFullName());
            lEquals = PojoUtil.equals(lEquals, this.getEmail(), lTest.getEmail());
            lEquals = PojoUtil.equals(lEquals, this.getQq(), lTest.getQq());
            lEquals = PojoUtil.equals(lEquals, this.getMsn(), lTest.getMsn());
            return lEquals;
        } else {
            return false;
        }
    }
    
    public int hashCode() {
        int result = 17;
        result = 37*result + ((this.id != null) ? this.id.hashCode() : 0);
        result = 37*result + ((this.userName != null) ? this.userName.hashCode() : 0);
        result = 37*result + ((this.password != null) ? this.password.hashCode() : 0);
        result = 37*result + ((this.fullName != null) ? this.fullName.hashCode() : 0);
        result = 37*result + ((this.email != null) ? this.email.hashCode() : 0);
        result = 37*result + ((this.qq != null) ? this.qq.hashCode() : 0);
        result = 37*result + ((this.msn != null) ? this.msn.hashCode() : 0);
        result = 37*result + ((this.dateCreated != null) ? this.dateCreated.hashCode() : 0);
        return result;
    }
    
    /**
     * Setter is needed in RollerImpl.storePersistentObject()
     */
    public void setData( Entity otherData ) {
        User other = (User)otherData;
        this.id =       other.getId();
        this.userName = other.getUserName();
        this.password = other.getPassword();
        this.fullName = other.getFullName();
        this.email = other.getEmail();
        this.qq = other.getQq();
        this.msn = other.getMsn();
        this.dateCreated = other.getDateCreated()!=null ? (Date)other.getDateCreated().clone() : null;
    }
    /**
     * is owner?
     * @param website
     * @return
     */
    public boolean isOwner(Website website){
    	if(website.getCreator().getEmail().equals(this.email)){
    		return true;
    	}else{
    		return false;
    	}
    }

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}
    
//    public Set<Role> getRoles() {
//        return roles;
//    }
//    
//    /**
//     * this is private to force the use of grant/revokeRole() methods.
//     */
//    public void setRoles(Set<Role> roles) {
//        this.roles = roles;
//    }
    
    
//    /**
//     * Returns true if user has role specified.
//     */
//    public boolean hasRole(String roleName) {
//        Iterator<Role> iter = roles.iterator();
//        while (iter.hasNext()) {
//            Role role = (Role) iter.next();
//            if (role.getName().equals(roleName)) {
//                return true;
//            }
//        }
//        return false;
//    }
    
    
//    /**
//     * Revokes specified role from user.
//     * @param roleName 
//     */
//    public void revokeRole(String roleName) throws Exception {
//        Role removeme = null;
//        Iterator<Role> iter = roles.iterator();
//        while (iter.hasNext()) {
//            Role role = (Role) iter.next();
//            if (role.getName().equals(roleName)) {
//                removeme = role;
//            }
//        }
//        /* 
//         * NOTE: we do this outside the loop above because we are not allowed
//         * to modify the contents of the Set while we are iterating over it.
//         * doing so causes a ConcurrentModificationException
//         */
//        if(removeme != null) {
//            roles.remove(removeme);
//        }
//    }
    
}
